package com.shen.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
 * hibernate工具类
 * @author shen
 *
 */
public class HibernateUtils {
	
	private static SessionFactory factory;
	
	static {
		try {
			Configuration cfg = new Configuration().configure();
			factory = cfg.buildSessionFactory();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得sessionFactory
	 * @return
	 */
	public static SessionFactory getSessionFactory() {
		return factory;
	}
	
	/**
	 * 获得session
	 * @return
	 */
	public static Session getSession() {
		return factory.openSession();
	}
	
	/**
	 * 关闭session
	 * @param session
	 */
	public static void closeSession(Session session) {
		if (session != null) {
			if (session.isOpen()) {
				session.close();
			}
		}
	}
}
